﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Text;

using BusinessLogic.BusinessRules;

namespace RichECMS
{
    public class Global : System.Web.HttpApplication, IRequiresSessionState
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            Model.Config.OnApplicationStart(Context.Server.MapPath(Context.Request.ApplicationPath));
            log4net.Config.DOMConfigurator.Configure();
        }

        #region 上传组件

        protected void Application_BeginRequest(object sender, EventArgs e)
        {
            /* we guess at this point session is not already retrieved by application so we recreate cookie with the session id... */
            try
            {
                string session_param_name = "ASPSESSID";
                string session_cookie_name = "ASP.NET_SessionId";

                if (HttpContext.Current.Request.Form[session_param_name] != null)
                {
                    UpdateCookie(session_cookie_name, HttpContext.Current.Request.Form[session_param_name]);
                }
                else if (HttpContext.Current.Request.QueryString[session_param_name] != null)
                {
                    UpdateCookie(session_cookie_name, HttpContext.Current.Request.QueryString[session_param_name]);
                }
            }
            catch
            {
            }

            try
            {
                string auth_param_name = "AUTHID";
                string auth_cookie_name = FormsAuthentication.FormsCookieName;

                if (HttpContext.Current.Request.Form[auth_param_name] != null)
                {
                    UpdateCookie(auth_cookie_name, HttpContext.Current.Request.Form[auth_param_name]);
                }
                else if (HttpContext.Current.Request.QueryString[auth_param_name] != null)
                {
                    UpdateCookie(auth_cookie_name, HttpContext.Current.Request.QueryString[auth_param_name]);
                }

            }
            catch
            {
            }
        }

        private void UpdateCookie(string cookie_name, string cookie_value)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies.Get(cookie_name);
            if (null == cookie)
            {
                cookie = new HttpCookie(cookie_name);
            }
            cookie.Value = cookie_value;
            HttpContext.Current.Request.Cookies.Set(cookie);
        }
        #endregion

        protected void Session_Start(object sender, EventArgs e)
        {
            Session.Timeout = 60;
        }

        //protected void Application_BeginRequest(object sender, EventArgs e)
        //{

        //}

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
    public class State
    {
        static string strScript = "<script type='text/javascript' > var topparwnt=window;setTimeout('checkParent(topparwnt)',2000);function checkParent(toppar) {if (toppar.parent == toppar) {toppar.location = '/login.aspx';}else{topparwnt=toppar.parent;checkParent(topparwnt)}};</script>";
        public static SessionUser LoginUser	// 登录用户信息
        {
            get
            {
                if (HttpContext.Current.Session["SessionUser"] == null)
                {
                    StringBuilder stringbuilderErrorMessage = new StringBuilder();
                    stringbuilderErrorMessage.Append("登录已过期，或没有进行正确的登录!");
                    stringbuilderErrorMessage.Append("<br />2秒后自动跳转 " + State.strScript);
                    //HttpContext.Current.Response.Redirect("/login.aspx");
                    HttpContext.Current.Response.Write(stringbuilderErrorMessage.ToString());
                    HttpContext.Current.Response.End();
                    return null;
                    //throw new IssueCommon.CustomException("登录已过期，或没有进行正确的登录，请尝试刷新重新登录!");
                }
                else
                {
                    return (SessionUser)HttpContext.Current.Session["SessionUser"];
                }
            }
            set
            {
                HttpContext.Current.Session["SessionUser"] = value;
            }
        }

        public static int intWSID			//站点Session
        {
            get
            {
                if (HttpContext.Current.Session["intWSID"] == null)
                {
                    StringBuilder stringbuilderErrorMessage = new StringBuilder();
                    stringbuilderErrorMessage.Append("登录已过期，或没有进行正确的登录!");
                    stringbuilderErrorMessage.Append("<br />2秒后自动跳转 " + State.strScript);
                    //HttpContext.Current.Response.Redirect("/login.aspx");
                    HttpContext.Current.Response.Write(stringbuilderErrorMessage.ToString());
                    HttpContext.Current.Response.End();
                    return -1;
                    //throw new IssueCommon.CustomException("登录已过期，或没有进行正确的登录，请尝试刷新重新登录!");
                }
                else
                {
                    return (int)HttpContext.Current.Session["intWSID"];
                }
            }
            set
            {

                HttpContext.Current.Session["intWSID"] = value;
            }
        }

        /// <summary>
        /// 将站点ID设置为空值
        /// </summary>
        public static void SetWSIDNull()
        {
            HttpContext.Current.Session["intWSID"] = null;
        }

        /// <summary>
        /// 判断是否站点ID为空
        /// </summary>
        public static bool IsWSIDNull()
        {
            if (HttpContext.Current.Session["intWSID"] == null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  将栏目ID设置为空值
        /// </summary>
        public static void SetSTIDNull()
        {
            HttpContext.Current.Session["intSTID"] = null;
        }

        /// <summary>
        /// 判断栏目ID是否为空值
        /// </summary>
        /// <returns></returns>
        public static bool IsSTIDNull()
        {
            if (HttpContext.Current.Session["intSTID"] == null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static int intSTID			//栏目Session
        {
            get
            {
                if (HttpContext.Current.Session["intSTID"] == null)
                {
                    StringBuilder stringbuilderErrorMessage = new StringBuilder();
                    stringbuilderErrorMessage.Append("登录已过期，或没有进行正确的登录!");
                    stringbuilderErrorMessage.Append("<br />2秒后自动跳转 " + State.strScript);
                    //HttpContext.Current.Response.Redirect("/login.aspx");
                    HttpContext.Current.Response.Write(stringbuilderErrorMessage.ToString());
                    HttpContext.Current.Response.End();
                    return -1;
                   // throw new IssueCommon.CustomException("登录已过期，或没有进行正确的登录，请尝试刷新重新登录!");
                }
                else
                {
                    return (int)HttpContext.Current.Session["intSTID"];
                }
            }
            set
            {
                HttpContext.Current.Session["intSTID"] = value;
            }
        }

        public static int intPageIndex			//记录列表当前页
        {
            get
            {
                if (HttpContext.Current.Session["intPageIndex"] == null)
                {
                    HttpContext.Current.Session["intPageIndex"] = 1;
                }
                return (int)HttpContext.Current.Session["intPageIndex"];
            }
            set
            {
                HttpContext.Current.Session["intPageIndex"] = value;
            }
        }

        public static int intLinkGroupID			//记录链接组ID
        {
            get
            {
                if (HttpContext.Current.Session["intLinkGroupID"] == null)
                {
                    HttpContext.Current.Session["intLinkGroupID"] = -1;
                }
                return (int)HttpContext.Current.Session["intLinkGroupID"];
            }
            set
            {
                HttpContext.Current.Session["intLinkGroupID"] = value;
            }
        }

        public static string strWhereSql //记录列表当前页
        {
            get
            {
                if (HttpContext.Current.Session["strWhereSql"] == null)
                {
                    HttpContext.Current.Session["strWhereSql"] = "";
                }
                return HttpContext.Current.Session["strWhereSql"].ToString();
            }
            set
            {
                HttpContext.Current.Session["strWhereSql"] = value;
            }
        }

        public static System.Collections.Hashtable PageControlState		//用于保存页面控件状态的HashTable
        {
            get
            {
                return (System.Collections.Hashtable)
                    HttpContext.Current.Session["PageControlState"];
            }
            set
            {
                HttpContext.Current.Session["PageControlState"] = value;
            }
        }
        public static string WebUrl
        {
            get
            {
                if (HttpContext.Current.Session["WebUrl"] == null)
                {
                    StringBuilder stringbuilderErrorMessage = new StringBuilder();
                    stringbuilderErrorMessage.Append("登录已过期，或没有进行正确的登录!");
                    stringbuilderErrorMessage.Append("<br />2秒后自动跳转 " + State.strScript);
                    //HttpContext.Current.Response.Redirect("/login.aspx");
                    HttpContext.Current.Response.Write(stringbuilderErrorMessage.ToString());
                    HttpContext.Current.Response.End();
                    return "";
                    // throw new IssueCommon.CustomException("登录已过期，或没有进行正确的登录，请尝试刷新重新登录!");
                }
                else
                {
                    return (string)HttpContext.Current.Session["WebUrl"];
                }
            }
            set
            {
                HttpContext.Current.Session["WebUrl"] = value;
            }
        }
        /// <summary>
        /// 判断当前
        /// </summary>
        public static bool IsManager
        {
            get
            {
                if (HttpContext.Current.Session["blnManager"] == null)
                {
                    StringBuilder stringbuilderErrorMessage = new StringBuilder();
                    stringbuilderErrorMessage.Append("登录已过期，或没有进行正确的登录!");
                    stringbuilderErrorMessage.Append("<br />2秒后自动跳转 " + State.strScript);
                    //HttpContext.Current.Response.Redirect("/login.aspx");
                    HttpContext.Current.Response.Write(stringbuilderErrorMessage.ToString());
                    HttpContext.Current.Response.End();
                    return false;
                    //throw new IssueCommon.CustomException("登录已过期，或没有进行正确的登录，请尝试刷新重新登录!");
                }
                else
                {
                    return (bool)HttpContext.Current.Session["blnManager"];
                }
            }
            set
            {
                HttpContext.Current.Session["blnManager"] = value;
            }
        }
    }
}